package com.ptr.ptrsystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import com.ptr.ptrsystem.logica.*;
import com.ptr.controladores.*;
import com.ptr.ptrsystem.dao.Lista;
import java.sql.*;

/**
 *
 * @author WALDIR
 */
@WebServlet(name = "MantenimientoMatrimonioServlet", urlPatterns = {"/MantenimientoMatrimonio"})
public class MantenimientoMatrimonioServlet extends HttpServlet {

    public MantenimientoMatrimonioServlet() {
    }
    String COD_PERSONA;
    String NRO_ACTA_MAT;
    String NRO_LIBRO;
    String FECH_REG_MAT;
    String IDUBIGEO_MAT;
    String DNI_PER_1;
    String DNI_PER_2;
    String DNI_TEST_1;
    String DNI_TEST_2;
    String DNI_REGISTRADOR;
    String COD_ESTADO;
    String IDUBIGEO;
    String COD_NACIONALIDAD;
    //
    String NLibro;
    String RUTA_ACTA_MAT;
    String RUTA_ACTA_MAT2;
    // 
    String depm;
    String provm;
    String distm;
    String otrom;
    String dep;
    String prov;
    String dist;
    String otro;
    conexionsqlserver con = new conexionsqlserver();
    Connection cn = null;
    Statement st = null;
    ResultSet rs = null;
    String QUERY = "";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        COD_PERSONA = request.getParameter("codpersona").trim();
        NRO_ACTA_MAT = request.getParameter("nroactaMat").trim();

        NRO_LIBRO = request.getParameter("NroLibro").trim();

        FECH_REG_MAT = request.getParameter("FRMatr").trim();
        //ubigeo matrimonial
        depm = request.getParameter("depm");
        provm = request.getParameter("provm");
        distm = request.getParameter("distm");
        otrom = request.getParameter("otrom");
        IDUBIGEO_MAT = depm + provm + distm + otrom;
        //
        DNI_PER_1 = request.getParameter("DNIP1").trim();
        DNI_PER_2 = request.getParameter("DNIP2").trim();
        DNI_TEST_1 = request.getParameter("DNIT1").trim();
        DNI_TEST_2 = request.getParameter("DNIT2").trim();
        DNI_REGISTRADOR = request.getParameter("DNIReg").trim();
        COD_ESTADO = request.getParameter("CodEsta").trim();
        //UBIGEO
        dep = request.getParameter("dep");
        prov = request.getParameter("prov");
        dist = request.getParameter("dist");
        otro = request.getParameter("otro");
        IDUBIGEO = dep + prov + dist + otro;
        //

        COD_NACIONALIDAD = request.getParameter("CodNac").trim();
        NLibro = request.getParameter("NLibro");
        String Ruta = request.getParameter("RAMat").trim();
        RUTA_ACTA_MAT = NLibro + "/" + Ruta;
        RUTA_ACTA_MAT2 =  request.getParameter("RAMat2").trim();;
//        }

        String BTN = request.getParameter("btn1");
        if (BTN.equals("guardarMatr")) {
            String Query = ("select * from TB_UBIGEO where idubigeo='" + IDUBIGEO + "'");
            conexionsqlserver con = new conexionsqlserver();
            Connection cn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                con.conectar();
                cn = con.getconection();
                st = cn.createStatement();
                rs = st.executeQuery(Query);
                if (rs.next()) {
                    GuardarMatrimonio(request, response);
                } else {
                    request.setAttribute("msj", "1");
                    getServletContext().getRequestDispatcher("/jsp/NMatrimonio.jsp").forward(request, response);
                }
            } catch (Exception b) {
                b.printStackTrace();
            }
        }
        if (BTN.equals("mostrarDNIP1")) {
            mostrardnip1(request, response);
        }
        if (BTN.equals("mostrarDNIP2")) {
            mostrardnip2(request, response);
        }
        if (BTN.equals("mostrarDNIT1")) {
            mostrardnit1(request, response);
        }
        if (BTN.equals("mostrarDNIT2")) {
            mostrardnit2(request, response);
        }
        if (BTN.equals("mostrarREGISTRADOR")) {
            mostrarREGISTRADOR(request, response);
        }
    }

    //
    protected void mostrardnip1(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(Lista.getNombrePersona(DNI_PER_1));
    }

    protected void mostrardnip2(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(Lista.getNombrePersona(DNI_PER_2));
    }
    //

    protected void mostrardnit1(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println(Lista.getNombrePersona(DNI_TEST_1));
    }
    //
    //

    protected void mostrardnit2(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println(Lista.getNombrePersona(DNI_TEST_2));
    }

    protected void mostrarREGISTRADOR(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println(Lista.getNombrePersona(DNI_REGISTRADOR));

    }
//

    protected void GuardarMatrimonio(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        LogicaMatrimonio matr = new LogicaMatrimonio();
        boolean IMat = matr.InsertarMatrimonio(COD_PERSONA, NRO_ACTA_MAT, NRO_LIBRO, FECH_REG_MAT, IDUBIGEO_MAT, DNI_PER_1, DNI_PER_2, DNI_TEST_1, DNI_TEST_2, DNI_REGISTRADOR, COD_ESTADO, IDUBIGEO, COD_NACIONALIDAD, RUTA_ACTA_MAT,RUTA_ACTA_MAT2);

        if (IMat) {
            request.setAttribute("msj", "2");
            getServletContext().getRequestDispatcher("/jsp/NMatrimonio.jsp").forward(request, response);
        } else {
            request.setAttribute("msj", "1");
            getServletContext().getRequestDispatcher("/jsp/NMatrimonio.jsp").forward(request, response);
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
